package com.itboye.ihomebank.activity.myzhujia.qianbao;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.activity.login.TimeCount;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;

import java.util.Observable;
import java.util.Observer;
/**
 * 忘记原支付密码(2.填写验证码)
 *
 */
public class ActivityWangJiPassTwo extends BaseOtherActivity implements Observer{
	View v_statusbar;
	ImageView close_icon;// 返回图片
	TextView add_shap_title_tv;// 标题
	TextView pass_two_phone;// 显示手机号
	TextView pass_two_ok;// 下一步

	TextView pass_two_time;// 验证码重新发送
	EditText pass_code;// 验证码

	private TimeCount time;// 倒计时

	private Intent intent;

	private String phone;// 接上页面传来的手机号
	private String yzm;// 接上页面传来的验证码（暂时收不到信息，验证码从上个页面传过来）
	
	private UserPresenter userPresenter;
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.pass_two_time:// 重新获取验证码
			time.start();

			userPresenter.send(phone, "7");
			break;
		case R.id.pass_two_ok:
			if (pass_code.getText().toString().trim() == null) {
				ByAlert.alert("请输入验证码");
				return;
			}
			userPresenter.verification(phone, "7",pass_code.getText().toString().trim());
			
//			Intent intent=new Intent(this, ActivityWangJiPassThree.class);
//			startActivity(intent);
			break;

		default:
			break;
		}
		
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_wangji_pass02;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onInit() {
		setStatusBarHeight(v_statusbar);
		add_shap_title_tv.setText("填写验证码");
		userPresenter = new UserPresenter(this);

		time = new TimeCount(60000, 1000, pass_two_time);// 构造CountDownTimer对象
		time.start();

		intent = super.getIntent();
		phone = intent.getStringExtra("phone");
		yzm = intent.getStringExtra("yzm");

		pass_two_phone.setText(phone);
		pass_code.setText(yzm);
		
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity=handlerError(data);
		if(entity!=null){
			if(!entity.getCode().equals(0)){
				if(entity.getEventType()==UserPresenter.verification_success){
					String bean=(String) entity.getData();
					if(bean!=null){
						ByAlert.alert("验证通过");
						Intent intent=new Intent(this,ActivityWangJiPassThree.class);
						intent.putExtra("phone", phone.trim());
						intent.putExtra("yzm", yzm.trim());
						startActivity(intent);
						finish();
					}else{
						ByAlert.alert("验证失败");
					}
				}if(entity.getEventType() == UserPresenter.verification_fail){
					ByAlert.alert("验证码错误");
				}
				if(entity.getEventType()==UserPresenter.send_success){
					String bean=(String) entity.getData();
					if(bean!=null){
						ByAlert.alert("验证码发送成功");
//						pass_code.setText(bean);
					}else{
						ByAlert.alert("失败");
					}
				}if(entity.getEventType() == UserPresenter.send_fail){
					ByAlert.alert("验证码发送失败");
				}
			}
		}
		
	}

}
